<template>
  <div class="display-table-div">
    <el-table
      :data="detailDataList"
	     class="display-table"
       height="50vh"
      :row-style="getRowStyle"
      :header-cell-style="header_cell_style"
    >
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="80"
      ></el-table-column>
      <template v-for="propItem in detailPropList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template #default="scope" v-if="propItem.valueFunc">
            {{ propItem.valueFunc(scope.row) }}
          </template>
          <template #default="scope" v-if="propItem.codeMap">
            {{ formatCodeToLabel(scope.row[propItem.prop] || propItem.defaultValue ,propItem.codeMap) }}
          </template>
          <template #default="scope" v-else-if="propItem.timeStampFormat">
            {{ formatStampToTime(scope.row[propItem.prop],propItem.timeStampFormat) }}
          </template>
          <template #default="scope" v-else>
            <slot :name="propItem.prop" :row="scope.row">
              {{ scope.row[propItem.prop] ?? propItem.defaultValue  }}
              <!-- 2023 6 2 此处不使用||,而使用?? 使用||,0也会被判断false -->
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          popper-class="detailselect"
          v-model:current-page="page.currentPage"
          v-model:page-size="page.pageSize"
          :page-sizes="[10, 20, 100, 200]"
          layout="total, sizes, prev, pager, next"
          :total="detailDataConut||0"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script>
import { computed } from "vue";
import moment from "moment";
export default {
  props: {
    detailDataList: {
      type: Array,
      required: true
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 })
    },
    detailPropList: {
      type: Array,
      required: true
    },
    detailDataConut: {
      type: Number,
      required: false,
      default:0
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    getRowStyle: {
      type: Function,
      default: ()=>{}
    },
    header_cell_style: {
      type: Object,
      default: ()=>({})
    },
    showFooter: {
      type: Boolean,
      default:true
    },
  },
  emits: ['selectionChange', 'update:page'],
  setup(props, { emit }) {
    const handleCurrentChange = (currentPage) => {
      emit('update:page', { ...props.page, currentPage })
    }
    const handleSizeChange = (pageSize) => {
      emit('update:page', { ...props.page, pageSize })
    }


    //格式转换
    const formatCodeToLabel = computed(()=>{
      return (title,codemap) => {
              return codemap.find(item=>item.value===title)?.title||title
            }
    })
    //时间戳转换
    const formatStampToTime= computed(()=>{
      return (stamp,format="YYYY-MM-DD HH:mm:ss")=>{
                if (stamp == 0 || stamp == null || stamp == "")
                    return '未设置';
                return moment(stamp)
                    .locale("zh-cn")
                    .format(format);
            }
    })
    return {
      handleSizeChange,
      handleCurrentChange,
      formatCodeToLabel,
      formatStampToTime
    }
  }
};
</script>

<style>
</style>
